package org.lmd.platform.controller.system;

import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.lmd.platform.annotation.PlLog;
import org.lmd.platform.controller.BaseController;
import org.lmd.platform.entity.sys.ReturnObjct;
import org.lmd.platform.entity.sys.UserEntity;
import org.lmd.platform.exception.PlatFormException;
import org.lmd.platform.shrio.ShiroUtils;
import org.lmd.platform.sys.UserService;
import org.lmd.platform.validator.Assert;
import org.lmd.platform.validator.ValidatorUtils;
import org.lmd.platform.validator.group.AddGroup;
import org.lmd.platform.validator.group.UpdateGroup;
import org.lmd.platform.vo.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.lmd.platform.shrio.ShiroUtils.getUserId;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public ReturnObjct list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return ReturnObjct.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public ReturnObjct info(){
        return ReturnObjct.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @PlLog("修改密码")
    @RequestMapping("/password")
    public ReturnObjct password(String password, String newPassword){
        Assert.isBlank(newPassword, "新密码不为能空");

        //原密码
        password = ShiroUtils.sha256(password, getUser().getSalt());
        //新密码
        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());

        //更新密码
        boolean flag = userService.updatePassword(getUserId(), password, newPassword);
        if(!flag){
            return ReturnObjct.error("原密码不正确");
        }

        return ReturnObjct.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public ReturnObjct info(@PathVariable("userId") Long userId){
        UserEntity user = userService.selectById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList =userService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return ReturnObjct.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @PlLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("sys:user:save")
    public ReturnObjct save(@RequestBody UserEntity user) throws PlatFormException {
        ValidatorUtils.validateEntity(user, AddGroup.class);

        userService.save(user);

        return ReturnObjct.ok();
    }

    /**
     * 修改用户
     */
    @PlLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("sys:user:update")
    public ReturnObjct update(@RequestBody UserEntity user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        userService.update(user);

        return ReturnObjct.ok();
    }

    /**
     * 删除用户
     */
    @PlLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public ReturnObjct delete(@RequestBody Long[] userIds){
        if(ArrayUtils.contains(userIds, 1L)){
            return ReturnObjct.error("系统管理员不能删除");
        }

        if(ArrayUtils.contains(userIds, getUserId())){
            return ReturnObjct.error("当前用户不能删除");
        }

        userService.deleteBatchIds(Arrays.asList(userIds));

        return ReturnObjct.ok();
    }
}
